package com.capinfo.controller;

import com.alibaba.fastjson.JSONObject;
import com.capinfo.base.BaseController;

import com.capinfo.entity.CapServiceDocument;
import com.capinfo.entity.SysFileInfo;
import com.capinfo.service.CapServiceDocumentService;
import com.capinfo.service.SysFileInfoService;
import com.capinfo.util.CommonUtil;
import com.capinfo.util.JsonUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author: sunbenjin
 * @date: 2020/2/29
 **/
@Controller
@RequestMapping(value="/document")
public class CapServiceDocumentController extends BaseController {

    @Autowired
    private CapServiceDocumentService capServiceDocumentService;

    @Autowired
    private SysFileInfoService sysFileInfoService;


    @ApiOperation(value="getDocumentList",httpMethod = "GET",notes="根据业务分务id得业务所需要的材料")
    @GetMapping("getDocumentList")
    @ResponseBody
    public JsonUtil getDocumentList(HttpServletRequest request){
        JsonUtil j = new JsonUtil();
        try {
            CapServiceDocument capServiceDocument = new CapServiceDocument();
            String serviceId = request.getParameter("serviceId");
            if(StringUtils.isNotEmpty(serviceId)){
                capServiceDocument.setServiceId(serviceId);
                List<CapServiceDocument> list = capServiceDocumentService.selectListByPage(capServiceDocument);
                List<CapServiceDocument> newList = new ArrayList<>();
                if(!CollectionUtils.isEmpty(list)){
                    String busId= request.getParameter("busId");
                    if(StringUtils.isNotEmpty(busId)){
                        for(CapServiceDocument document:list){
                            SysFileInfo sysFileInfo = new SysFileInfo();
                            sysFileInfo.setBusId(busId);
                            sysFileInfo.setServiceId(document.getId());
                            List<SysFileInfo> fileInfos = sysFileInfoService.selectListByPage(sysFileInfo);
                            document.setFileInfos(fileInfos);
                            newList.add(document);
                        }
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("data",newList);
                        j.setJsonObj(jsonObject);
                    }else{
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("data",list);
                        j.setJsonObj(jsonObject);
                    }
                    j.setMsg("获取所需材料列表成功");
                }else{
                    j.setMsg("无材料列表");
                    j.setFlag(Boolean.FALSE);
                }
            }else{
                j.setMsg("无材料列表");
                j.setFlag(Boolean.FALSE);
            }

        } catch (Exception e) {
            j.setMsg("获取失败");
            j.setFlag(Boolean.FALSE);

            e.printStackTrace();
        }
        return j;
    }

    @ApiOperation(value="batchHandleFile",httpMethod = "POST",notes="批量更新或插入所需材料")
    @PostMapping("batchHandleFile")
    @ResponseBody
    public JsonUtil batchUpdateFile(@RequestBody List<CapServiceDocument> docInfoList){
        JsonUtil j = new JsonUtil();
        String userId = CommonUtil.getUser().getId();
        try {
            if(CollectionUtils.isNotEmpty(docInfoList)){
                List<CapServiceDocument> insertList = docInfoList.stream().filter((doc)->StringUtils.equals(doc.getDocFlag(),"0")).collect(Collectors.toList());
                List<CapServiceDocument> updateList = docInfoList.stream().filter((doc)->StringUtils.equals(doc.getDocFlag(),"1")).collect(Collectors.toList());
                if(CollectionUtils.isNotEmpty(insertList)){
                    insertList.forEach((doc)->doc.setId(UUID.randomUUID().toString().replace("-","")));
                    capServiceDocumentService.batchInsert(insertList);
                }
                if(CollectionUtils.isNotEmpty(updateList)){
                    capServiceDocumentService.batchUpdate(updateList);

                }
            }
            j.setFlag(true);
            j.setMsg("保存成功");
        } catch (Exception e) {
            j.setMsg("保存失败");
            j.setFlag(false);

            e.printStackTrace();
        }
        return j;
    }
}
